// 实验5 事件组

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_log.h"

// 1. 创建事件组
static EventGroupHandle_t my_event;
#define EVENT1BIT BIT0
#define EVENT2BIT BIT2

// 2. 产生事件(发送)
void taskA(void *param)
{
    while (1)
    {
        // ESP_LOGI("main","hello world!");
        xEventGroupSetBits(my_event, EVENT1BIT);
        xEventGroupSetBits(my_event, EVENT2BIT);
        vTaskDelay(pdMS_TO_TICKS(500));
        xEventGroupSetBits(my_event, EVENT2BIT);
        vTaskDelay(pdMS_TO_TICKS(500));
    }
}

// 3. 事件响应(接收)
void taskB(void *param)
{
    EventBits_t ev; // 等待事件位
    while (1)
    {
        // 获取事件(阻塞)
        ev = xEventGroupWaitBits(my_event, EVENT1BIT | EVENT2BIT, pdTRUE, pdFALSE, pdMS_TO_TICKS(5000));
        if (ev & EVENT1BIT)
        {
            ESP_LOGI("event", "get BIT0 event!");
        }
        if (ev & EVENT2BIT)
        {
            ESP_LOGI("event", "get BIT2 event!");
        }
    }
}

void app_main(void)
{
    // 0. 初始化事件组
    my_event = xEventGroupCreate();

    xTaskCreatePinnedToCore(taskA, "taskA", 2048, NULL, 3, NULL, 1);
    xTaskCreatePinnedToCore(taskB, "taskB", 2048, NULL, 2, NULL, 1);
}
